package com.youlai.boot.factory.controller;

import com.youlai.boot.factory.service.ProcessTemplateService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.youlai.boot.factory.model.form.ProcessTemplateForm;
import com.youlai.boot.factory.model.query.ProcessTemplateQuery;
import com.youlai.boot.factory.model.vo.ProcessTemplateVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.boot.common.result.PageResult;
import com.youlai.boot.common.result.Result;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;

/**
 * 工序模板主前端控制层
 *
 * @author youlaitech
 * @since 2025-10-01 05:11
 */
@Tag(name = "工序模板主接口")
@RestController
@RequestMapping("/api/v1/process-template")
@RequiredArgsConstructor
public class ProcessTemplateController  {

    private final ProcessTemplateService processTemplateService;

    @Operation(summary = "工序模板主分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('factory:process-template:query')")
    public PageResult<ProcessTemplateVO> getProcessTemplatePage(ProcessTemplateQuery queryParams ) {
        IPage<ProcessTemplateVO> result = processTemplateService.getProcessTemplatePage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增工序模板主")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('factory:process-template:add')")
    public Result<Void> saveProcessTemplate(@RequestBody @Valid ProcessTemplateForm formData ) {
        boolean result = processTemplateService.saveProcessTemplate(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取工序模板主表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('factory:process-template:edit')")
    public Result<ProcessTemplateForm> getProcessTemplateForm(
        @Parameter(description = "工序模板主ID") @PathVariable Long id
    ) {
        ProcessTemplateForm formData = processTemplateService.getProcessTemplateFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改工序模板主")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('factory:process-template:edit')")
    public Result<Void> updateProcessTemplate(
            @Parameter(description = "工序模板主ID") @PathVariable Long id,
            @RequestBody @Validated ProcessTemplateForm formData
    ) {
        boolean result = processTemplateService.updateProcessTemplate(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除工序模板主")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('factory:process-template:delete')")
    public Result<Void> deleteProcessTemplates(
        @Parameter(description = "工序模板主ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = processTemplateService.deleteProcessTemplates(ids);
        return Result.judge(result);
    }
}
